package com.xy.netty.dome.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 1
 */
@Slf4j
public class EchoServiceLengthFieldBasedHandler extends ChannelInboundHandlerAdapter {
    private  int countNum=0;
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //第一种
        //Channel channel = ctx.channel();
        //channel.writeAndFlush(Unpooled.copiedBuffer("发送信息测试",CharsetUtil.UTF_8));

        //第二种
        //ChannelPipeline channelPipeline = ctx.pipeline();
        //channelPipeline.writeAndFlush(Unpooled.copiedBuffer("发送信息测试",CharsetUtil.UTF_8));
//        String sendMsg="服务端发送信息测试";
//        ctx.channel().writeAndFlush(Unpooled.copiedBuffer(sendMsg,CharsetUtil.UTF_8));
//        log.info("服务端发送数据：{}",sendMsg);
        readOneMsg4(ctx,msg);
        countNum++;
    }

    /**
     * 读取 案列：1 信息操作方法
     * @param ctx
     * @param msg
     * @throws Exception
     */
    private void readOneMsg(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf) msg;
        final int len = buf.readShort();
        byte[] data = new byte[len];
        buf.readBytes(data, 0, len);
        final String content = new String(data, CharsetUtil.UTF_8);
        log.info("服务端收到数据：{},接收信息序列号：{}，长度：{}",content,countNum,len);
    }



    /*例子4*/
    private void readOneMsg4(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf) msg;
        final int header1 = buf.readShort();
        final int len = buf.readMedium();
        byte[] data = new byte[len];
        buf.readBytes(data, 0, len);
        final String content = new String(data,CharsetUtil.UTF_8);
        log.info("服务端收到数据：{},接收信息序列号：{}，长度：{},header：{}",content,countNum,len,header1);
    }

    /*例子5*/
    private void readOneMsg5(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf) msg;
        final int len = buf.readMedium();
        System.out.printf("len=%#x\n", len);
        final int header1 = buf.readShort();
        System.out.printf("header1=%#x\n", header1);
        byte[] data = new byte[len];
        buf.readBytes(data, 0, len);
        final String str = new String(data, CharsetUtil.UTF_8);
        System.out.printf("len=%s\n", str);
    }
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        log.info("EchoServiceLengthFieldBasedHandler channelReadComplete");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("EchoServiceLengthFieldBasedHandler exceptionCaught",cause);
        ctx.close();
    }


    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        log.info("EchoServiceLengthFieldBasedHandler channelRegistered");
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        log.info("EchoServiceLengthFieldBasedHandler channelUnregistered");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("EchoServiceLengthFieldBasedHandler channelActive");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info("EchoServiceLengthFieldBasedHandler channelInactive");
    }
}
